<template>
  <Modal v-model="modal" :title="title">
    <div class="public-setting-list-modal h400 scroll">
      <div class="title">
        <div class="store">门店类型</div>
        <div><span class="font-pink">*</span> 无扫码进货记录时限 单位:天</div>
        <div><span class="font-pink">*</span>无中策云店抢购订单 单位:天</div>
        <div><span class="font-pink">*</span>在前面的扫码天数内扫码进货记录小于多少条 单位:天</div>
      </div>
      <div class="item" v-for="(item, index) in storeTypeList" :key="index">
        <div class="store">{{item.name}}</div>
        <InputNumber style="widht:100;" :max="99999" :min="0" v-model="item.nonBarcodeDays"></InputNumber>
        <InputNumber style="widht:100;" :max="99999" :min="0" v-model="item.nonPurchaseDays"></InputNumber>
        <InputNumber style="widht:100;" :max="99999" :min="0" v-model="item.lessThanPurchQuantity"></InputNumber>
      </div>
    </div>
    <Loader :status="status" @refresh="initData"></Loader>
    <div slot="footer">
        <Button @click="modal = false" size="large" type="text">取消</Button>
        <Button @click="onSave" type="primary" size="large">确定</Button>
    </div>
</Modal>
</template>
<script>
export default {
  data() {
    return {
      id: null,
      modal: false,
      title: '公共资源库设置',
      options: {
        tableHeight: 200
      },
      storeTypeList: [],
      list: [
        {lessThanPurchQuantity:''}
      ],
      status: 'pass'
    };
  },
  methods: {
    async initData() {
      try {
        this.status = 'loading'
        await this.getRules()
        await this.getStoreTypeList()
        this.status = 'pass'
      } catch (error) {
        this.status = 'error'
      }
    },
    getRules () {
      return this.$ajaxGet('/setting/public_resource_config/list').then(res => {
        this.list = res
      })
    },
    getStoreTypeList() {
      return this.$ajaxGet('/storeType/findAll').then(res => {
        this.storeTypeList = res.map(item => {
          let hv = this.list.find(i => i.storeTypeId === item.id) || {}
          item.nonBarcodeDays = hv.nonBarcodeDays || 0
          item.nonPurchaseDays = hv.nonPurchaseDays || 0
          item.lessThanPurchQuantity = hv.lessThanPurchQuantity
          return item
        })
      })
    },
    openModal() {
      this.modal = true;
      this.initData()
    },
    closeModal() {
      this.modal = false;
    },
    onSave() {  
      let list = this.storeTypeList.map(item => {
        const { nonBarcodeDays, lessThanPurchQuantity, nonPurchaseDays, id: storeTypeId } = item;
        return { nonBarcodeDays,lessThanPurchQuantity, nonPurchaseDays, storeTypeId }
      })
      for(let i=0; i<list.length-1;i++){
        if(!list[i].lessThanPurchQuantity && list[i].lessThanPurchQuantity !='0'){
         this.$Message.error('在前面的扫码天数内扫码进货记录小于多少条天数不能为空')
         return
        }
      }
      this.$jsonPost('/setting/public_resource_config/save', list).then(res => {
          this.$Message.info('保存成功')
      })
          this.modal = false;
    }    
  }
}
</script>

